package com.rocksea.app.fragment;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

import com.rocksea.app.BaseFragment;
import com.rocksea.app.R;
import com.rocksea.app.activity.BaseShowActivity;
import com.rocksea.app.db.DbCenter;
import com.rocksea.app.db.lowStrain.LowStrainDb;
import com.rocksea.app.event.NotificationValid;
import com.rocksea.app.interfaces.MyExecuteCallBack;
import com.rocksea.app.tasks.AskInfoTask;
import com.rocksea.app.weight.LowStrainDbView;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


import butterknife.InjectView;

/**
 * @author Administrator
 * @date 2017/11/21
 */

public class LowStrainViewFragment extends BaseFragment
{
    public static final int VALID = 8;
    public static final int UN_VALID =9;
    public static final String VALID_KEY = "valid_key";

    @InjectView(R.id.Low_Strain_View)
    LowStrainDbView mLowStrainDbView;

    @InjectView(R.id.Pile_Demi)
    TextView pileDemi;
    @InjectView(R.id.Pile_Length)
    TextView pileLength;
    @InjectView(R.id.Pile_V)
    TextView v;


    private String baseInfoId;

    public static LowStrainViewFragment newInstance(Bundle args)
    {
        LowStrainViewFragment fragment = new LowStrainViewFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    protected void initView()
    {
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
        setHasOptionsMenu(true);
        Bundle bundle = getArguments();
        baseInfoId = bundle.getString(BaseShowActivity.BASEINFOID_KEY);
        mLowStrainDbView.setOnItemValidChangedListener(new LowStrainDbView.OnItemValidChangedListener()
        {
            @Override
            public void onChanged(String uuid, boolean isValid) {
                if (isValid)
                    DbCenter.makeAsIsValid(uuid,0);
                else
                {
                    DbCenter.makeAsIsValid(uuid,1);
                    EventBus.getDefault().post(new NotificationValid(false,NotificationValid.UN_VALID));
                }
            }
        });

        loading();
    }

    @Override
    protected void initData()
    {
        loading();
        AskInfoTask<LowStrainDb> askInfoTask = new AskInfoTask<>(baseInfoId,callBack);
        askInfoTask.execute();
    }

    @Override
    protected void requestAgain()
    {
        initData();
    }

    @Override
    protected int getContentViewId()
    {
        return R.layout.lowstrain_fragment_view_layout;
    }

    private MyExecuteCallBack<LowStrainDb> callBack = new MyExecuteCallBack<LowStrainDb>() {
        @Override
        public void onError(int code, String message) {
            loadFailed();
            mErrorText.setText(message);
        }

        @Override
        public void onSuccess(LowStrainDb lowStrainDb)
        {
            if (lowStrainDb.lowStrainDataDb.size()==0)
            {
                pileLength.setText("桩长 --m");
                pileDemi.setText("桩径 --mm");
                v.setText("波速 --m/s");
                loadFailed();
                mErrorText.setText("未查询到数据");
                return;
            }
            loadCompleted();
            pileLength.setText(String.format("桩长 %dm",lowStrainDb.pileLength));
            pileDemi.setText(String.format("桩径 %.2fmm",lowStrainDb.pileDiameter));
            v.setText(String.format("波速 %dm/s",lowStrainDb.velocity));
            mLowStrainDbView.setLowStrainDb(lowStrainDb,true);
        }
    };


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
    {
        inflater.inflate(R.menu.edit,menu);

        super.onCreateOptionsMenu(menu,inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.edit:
                if (item.getTitle().equals("编辑"))
                {
                    mLowStrainDbView.setRecycleViewPattern(LowStrainDbView.PATTERN_EDIT);
                    item.setTitle("取消");
                }
                else
                {
                    mLowStrainDbView.setRecycleViewPattern(LowStrainDbView.PATTERN_NORMAL);
                    item.setTitle("编辑");
                }

                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void notifaction(NotificationValid notificationValid)
    {
        if (notificationValid.getType()==NotificationValid.VALID)
        {
            initData();
        }

    }

}
